package com.limox.bishe.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.limox.bishe.R;
import com.limox.bishe.activity.ClassroomActivity;
import com.limox.bishe.activity.Setting;
import com.limox.bishe.activity.kbActivity;
import com.limox.bishe.app.APP;

import android.content.Context;
import android.content.Intent;
import android.provider.VoicemailContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView.FindListener;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class kbinfoAdapter extends BaseExpandableListAdapter {

	private Context context;
	private ArrayList<HashMap<String, String>> kblist;
	private Boolean isnowday;
	// private ArrayList<HashMap<String, String>> kblist_child;
	public List<ArrayList<HashMap<String, String>>> childlist;

	public kbinfoAdapter(Context context,
			ArrayList<HashMap<String, String>> kb,
			List<ArrayList<HashMap<String, String>>> child, Boolean isNowDay) {
		this.context = context;
		kblist = kb;
		childlist = child;
		isnowday = isNowDay;

	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childlist.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater mInflater = LayoutInflater.from(context);
		// 产生一个View
		View view = null;

		view = mInflater.inflate(R.layout.kb_list_item_child, null);

		// TextView
		// text_jieshu=(TextView)view.findViewById(R.id.text_jieshu);
		TextView text_secondlesson = (TextView) view
				.findViewById(R.id.secondlesson);
		final TextView text_courname = (TextView) view
				.findViewById(R.id.text_cour_name);
		final TextView text_teacher = (TextView) view.findViewById(R.id.teacher);
		final TextView text_classroom = (TextView) view.findViewById(R.id.classroom);
		TextView text_week = (TextView) view.findViewById(R.id.week);
		TextView text_remark = (TextView) view.findViewById(R.id.remark);
		TextView text_weekstart = (TextView) view
				.findViewById(R.id.text_weekstart);
		TextView text_weekend = (TextView) view.findViewById(R.id.text_weekend);
		TextView text_jieshu = (TextView) view.findViewById(R.id.text_jieshu);
		ProgressBar pro = (ProgressBar) view
				.findViewById(R.id.vertical_progressbar);

		// text_secondlesson.setText(childlist.get(groupPosition).toString());

		String lessontimeStr = childlist.get(groupPosition).get(childPosition)
				.get("time");

		int lessontime = Integer.valueOf(lessontimeStr.substring(
				lessontimeStr.indexOf("第") + 1, lessontimeStr.indexOf(",")));

		if (lessontime != 9) {
			text_jieshu.setText("0"
					+ lessontimeStr.substring(lessontimeStr.indexOf("第") + 1,
							lessontimeStr.indexOf("节")).replace(",", "-"));
		} else {
			text_jieshu.setText(lessontimeStr.substring(
					lessontimeStr.indexOf("第") + 1, lessontimeStr.indexOf("节"))
					.replace(",", "-"));
		}

		/**
		 * 设置周数
		 */

		pro.setVisibility(View.VISIBLE);
		// 获取其实周，结束周
		String weekStart = childlist.get(groupPosition).get(childPosition)
				.get("week");

		if (weekStart.indexOf("|") != -1) {
			// 有单双周的数据
			text_week.setText(weekStart.substring(weekStart.indexOf("|") + 1,
					weekStart.length() - 1));
		} else {
			text_week.setText("无");
			text_week.setTextColor(0x00000000);
			text_week.setBackgroundColor(0x00000000);
		}

		int weekend = Integer.valueOf(weekStart.substring(
				weekStart.indexOf("-") + 1, weekStart.indexOf("周")));
		int weekstart_int = Integer.valueOf(weekStart.substring(
				weekStart.indexOf("第") + 1, weekStart.indexOf("-")));

		text_weekend.setText(String.valueOf(weekend) + "周");
		text_weekstart.setText(String.valueOf(weekstart_int) + "周");

		pro.setMax(weekend - weekstart_int);
		// 课程还没开始或者课程已经结束
		if (APP.getWeekNow() <= weekstart_int) {
			pro.setProgress(weekend - weekstart_int);
		} else if (APP.getWeekNow() > weekend) {
			pro.setProgress(0);
		} else {
			pro.setProgress(pro.getMax() - APP.getWeekNow() + weekstart_int);
		}

		text_courname.setText(childlist.get(groupPosition).get(childPosition)
				.get("cour_name"));
		// text_time.setText(kblist.get(i).get("time"));
		text_teacher.setText(childlist.get(groupPosition).get(childPosition)
				.get("teacher"));
		text_classroom.setText(childlist.get(groupPosition).get(childPosition)
				.get("classroom"));
		text_remark.setText(childlist.get(groupPosition).get(childPosition)
				.get("remark"));

		Animation animation = new RotateAnimation(120f, 0f, 0f, 0f);
		animation.setDuration(1000);
		animation.setInterpolator(new OvershootInterpolator(1f));
		if (convertView == null) {
			view.setAnimation(animation);

		}
		
		
		//右边的进入教室按钮
		ImageButton imgToDoor = (ImageButton) view
				.findViewById(R.id.but_toroom);
		imgToDoor.setVisibility(View.VISIBLE);
		imgToDoor.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent gointent=new Intent();
				gointent.setClass(context,ClassroomActivity.class);
				gointent.putExtra("classroom",text_classroom.getText().toString());
				gointent.putExtra("teacher",text_teacher.getText().toString());
				gointent.putExtra("courname",text_courname.getText().toString());
				 
				context.startActivity(gointent);
				 
			}
		});
		
		
		return view;

	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub

		return childlist.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return kblist.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		// return kblist.size();
		return kblist.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int position, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater mInflater = LayoutInflater.from(context);
		// 产生一个View
		View view = null;
		view = mInflater.inflate(R.layout.kb_list_item, null);
		// TextView
		// text_jieshu=(TextView)view.findViewById(R.id.text_jieshu);
		TextView text_secondlesson = (TextView) view
				.findViewById(R.id.secondlesson);
		final TextView text_courname = (TextView) view
				.findViewById(R.id.text_cour_name);
		final TextView text_teacher = (TextView) view.findViewById(R.id.teacher);
		final TextView text_classroom = (TextView) view.findViewById(R.id.classroom);
		TextView text_week = (TextView) view.findViewById(R.id.week);
		TextView text_remark = (TextView) view.findViewById(R.id.remark);
		TextView text_weekstart = (TextView) view
				.findViewById(R.id.text_weekstart);
		TextView text_weekend = (TextView) view.findViewById(R.id.text_weekend);
		TextView text_jieshu = (TextView) view.findViewById(R.id.text_jieshu);
		ProgressBar pro = (ProgressBar) view
				.findViewById(R.id.vertical_progressbar);

		Button but_second = (Button) view.findViewById(R.id.but_second);

		LinearLayout linear_cour = (LinearLayout) view
				.findViewById(R.id.linear_cour);
		// 判断时间确定当前时间
		Date date = new Date();
		String time = String.format("%tR", date); // 得到当前时间
		String timeThisLesson = APP.getLessonTime(position); // 当前课程的时间

		// 选出比当前时间大的
		if (isnowday) {
			if (Integer.valueOf(timeThisLesson.substring(0,
					timeThisLesson.indexOf(":"))) < Integer.valueOf(time
					.substring(0, timeThisLesson.indexOf(":")))
					|| (Integer.valueOf(timeThisLesson.substring(0,
							timeThisLesson.indexOf(":"))) == Integer
							.valueOf(time.substring(0,
									timeThisLesson.indexOf(":"))) && Integer
							.valueOf(timeThisLesson.substring(timeThisLesson
									.indexOf(":") + 1)) < Integer.valueOf(time
							.substring(timeThisLesson.indexOf(":") + 1))

					)) {
				// 再选出小的，排除最后一节课
				if (position + 1 != APP.getJieCount()) {
					String timeNextLesson = APP.getLessonTime(position + 1);

					if (Integer.valueOf(timeNextLesson.substring(0,
							timeNextLesson.indexOf(":"))) > Integer
							.valueOf(time.substring(0, time.indexOf(":")))
							|| (Integer.valueOf(timeNextLesson.substring(0,
									timeNextLesson.indexOf(":"))) == Integer
									.valueOf(time.substring(0,
											time.indexOf(":"))) &&

							Integer.valueOf(timeNextLesson
									.substring(timeNextLesson.indexOf(":") + 1)) > Integer
									.valueOf(time.substring(time.indexOf(":") + 1))

							)

					) {

						view.setBackgroundColor(0x5582c6f6); // 设置当前课程的背景颜色
					}

				} else {
					view.setBackgroundColor(0x5582c6f6); // 设置当前课程的背景颜色

				}

			}
		}

		if (kblist.get(position) != null) { // 这节课有课
			
			
			
			String lessontimeStr = kblist.get(position).get("time");
			linear_cour.setVisibility(View.VISIBLE);
			int lessontime = Integer
					.valueOf(lessontimeStr.substring(
							lessontimeStr.indexOf("第") + 1,
							lessontimeStr.indexOf(",")));

			if (lessontime != 9) {
				text_jieshu.setText("0"
						+ lessontimeStr.substring(
								lessontimeStr.indexOf("第") + 1,
								lessontimeStr.indexOf("节")).replace(",", "-"));
			} else {
				text_jieshu.setText(lessontimeStr.substring(
						lessontimeStr.indexOf("第") + 1,
						lessontimeStr.indexOf("节")).replace(",", "-"));
			}

			/**
			 * 设置周数
			 */

			pro.setVisibility(View.VISIBLE);
			// 获取其实周，结束周
			String weekStart = kblist.get(position).get("week");

			// 提取是否单双周
			if (weekStart.indexOf("|") != -1) {
				// 有单双周的数据
				text_week.setVisibility(View.VISIBLE);
				text_week.setText(weekStart.substring(
						weekStart.indexOf("|") + 1, weekStart.length() - 1));
			} else {
				text_week.setVisibility(View.VISIBLE);
				text_week.setBackgroundColor(0x00000000);
				text_week.setText("无");
				text_week.setTextColor(0x00000000);
			}

			int weekend = Integer.valueOf(weekStart.substring(
					weekStart.indexOf("-") + 1, weekStart.indexOf("周")));
			int weekstart_int = Integer.valueOf(weekStart.substring(
					weekStart.indexOf("第") + 1, weekStart.indexOf("-")));

			text_weekend.setText(String.valueOf(weekend) + "周");
			text_weekstart.setText(String.valueOf(weekstart_int) + "周");

			pro.setMax(weekend - weekstart_int);
			// 课程还没开始或者课程已经结束
			if (APP.getWeekNow() <= weekstart_int) {
				pro.setProgress(weekend - weekstart_int);
			} else if (APP.getWeekNow() > weekend) {
				pro.setProgress(0);
			} else {
				pro.setProgress(pro.getMax() - APP.getWeekNow() + weekstart_int);
			}

			text_courname.setText(kblist.get(position).get("cour_name"));
			// text_time.setText(kblist.get(i).get("time"));
			text_teacher.setText(kblist.get(position).get("teacher"));
			text_classroom.setText(kblist.get(position).get("classroom"));
			text_remark.setText(kblist.get(position).get("remark"));

			// 同一时段还有其他课
			if (getChildrenCount(position) != 0) {
				but_second.setVisibility(View.VISIBLE);
			}
			
			//右边的进入教室按钮
			ImageButton imgToDoor = (ImageButton) view
					.findViewById(R.id.but_toroom);
			imgToDoor.setVisibility(View.VISIBLE);
			imgToDoor.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent gointent=new Intent();
					gointent.setClass(context,ClassroomActivity.class);
					
 					gointent.putExtra("classroom",text_classroom.getText().toString());
					gointent.putExtra("teacher",text_teacher.getText().toString());
					gointent.putExtra("courname",text_courname.getText().toString());
					 
 					
					context.startActivity(gointent);
					 
				}
			});

			
			
			

		} else { // 无课
			if (2 * position + 1 != 9) {
				text_jieshu.setText("0" + String.valueOf(2 * position + 1)
						+ "-" + String.valueOf(2 * position + 2));
			} else {
				text_jieshu.setText(String.valueOf(2 * position + 1) + "-"
						+ String.valueOf(2 * position + 2));
			}

		}

		// 第一行去掉顶部的分割线
		if (position == 0) {
			TextView text_divier = (TextView) view
					.findViewById(R.id.text_divider);
			text_divier.setVisibility(View.GONE);
		}
		
		 
		
		return view;

	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
